﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using GMap.NET.WindowsPresentation;

namespace InteractiveMapApp.CustomMarkers
{
    /// <summary>
    /// Interaction logic for blockCoromandel.xaml
    /// </summary>
    public partial class blockCoromandel
    {
        Popup Popup;
        Label Label;
        GMapMarker Marker;
        MainWindow MainWindow;

        public blockCoromandel(MainWindow window, GMapMarker marker, string title)
        {
            this.InitializeComponent();

            this.MainWindow = window;
            this.Marker = marker;

            Popup = new Popup();
            Label = new Label();

            this.Unloaded += new RoutedEventHandler(blockCoromandel_Unloaded);
            this.Loaded += new RoutedEventHandler(blockCoromandel_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(blockCoromandel_SizeChanged);

            //this.MouseEnter += new MouseEventHandler(MarkerControl_MouseEnter);
            //this.MouseLeave += new MouseEventHandler(MarkerControl_MouseLeave);
            //this.MouseMove += new MouseEventHandler(blockCoromandel_MouseMove);

            this.MouseUp += new MouseButtonEventHandler(blockCoromandel_MouseUp);
            MainWindow.MainMap.MouseDown += new MouseButtonEventHandler(mainMap_mouseDown);
            MainWindow.MainMap.MouseWheel += new MouseWheelEventHandler(MainMap_MouseWheel);

            Popup.Placement = PlacementMode.Mouse;
            {
                //Label.Background = Brushes.Blue;
                ImageBrush myBrush = new ImageBrush();
                //myBrush.ImageSource = new BitmapImage(new Uri(@"../../CustomMarkers/heart_icon.png", UriKind.Relative));
                myBrush.TileMode = TileMode.None;
                myBrush.Stretch = Stretch.Fill;
                //myBrush.Viewport = new Rect(0, 0, 420, 420);
                myBrush.ViewportUnits = BrushMappingMode.Absolute;

                Label.Background = myBrush;

                Label.Foreground = Brushes.White;
                Label.BorderBrush = Brushes.WhiteSmoke;
                Label.BorderThickness = new Thickness(2);
                Label.Padding = new Thickness(5);
                Label.FontSize = 22;
                Label.Content = title;
            }
            Popup.Child = Label;
        }

        void blockCoromandel_Loaded(object sender, RoutedEventArgs e)
        {
            if (icon.Source.CanFreeze)
            {
                icon.Source.Freeze();
            }
        }

        void blockCoromandel_Unloaded(object sender, RoutedEventArgs e)
        {
            this.Unloaded -= new RoutedEventHandler(blockCoromandel_Unloaded);
            this.Loaded -= new RoutedEventHandler(blockCoromandel_Loaded);
            this.SizeChanged -= new SizeChangedEventHandler(blockCoromandel_SizeChanged);
            //this.MouseEnter -= new MouseEventHandler(MarkerControl_MouseEnter);
            //this.MouseLeave -= new MouseEventHandler(MarkerControl_MouseLeave);
            //this.MouseMove -= new MouseEventHandler(blockCoromandel_MouseMove);
            MainWindow.MainMap.MouseDown -= new MouseButtonEventHandler(mainMap_mouseDown);
            MainWindow.MainMap.MouseWheel -= new MouseWheelEventHandler(MainMap_MouseWheel);
            this.MouseUp -= new MouseButtonEventHandler(blockCoromandel_MouseUp);

            Marker.Shape = null;
            icon.Source = null;
            icon = null;
            Popup = null;
            Label = null;
        }

        void blockCoromandel_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Marker.Offset = new Point(-e.NewSize.Width / 2, -e.NewSize.Height);
        }

        //void blockCoromandel_MouseMove(object sender, MouseEventArgs e)
        //{
        //   if(e.LeftButton == MouseButtonState.Pressed && IsMouseCaptured)
        //   {
        //      Point p = e.GetPosition(MainWindow.MainMap);
        //      Marker.Position = MainWindow.MainMap.FromLocalToLatLng((int) (p.X), (int) (p.Y));
        //   }
        //}
        //void MarkerControl_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    //hideMarker();
        //}

        //void MarkerControl_MouseEnter(object sender, MouseEventArgs e)
        //{
        //   //showMarker();
        //}

        void blockCoromandel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //showMarker();
        }

        void mainMap_mouseDown(object sender, MouseButtonEventArgs e)
        {
            //hideMarker();
        }

        void MainMap_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            //hideMarker();
        }

        void showMarker()
        {
            Marker.ZIndex += 10000;
            Popup.IsOpen = true;
        }
        void hideMarker()
        {
            Marker.ZIndex -= 10000;
            Popup.IsOpen = false;
        }
    }
}